/**
 * 
 */
package org.seginfo.httpfuzzer.header;

import java.io.Serializable;

/**
 * @author Administrador
 *
 */
public class FormatStringStrategy extends CommonStrategy implements AttackStrategy, Serializable {

	/**
	 * Devuelve un string que representa un entero de tamaño <i>size</i>
	 */
	public String getIntegerAttack(int size) {
		StringBuffer buffer = new StringBuffer();
		for (int i=0; i<(int) (Math.random() * size);i++) {
			buffer.append((char) (48 + ((int) Math.random()*10)));
		}
		return buffer.toString();
	}

	/**
	 * Devuelve un string que representa un entero
	 */
	public String getIntegerAttack() {
		return getIntegerAttack((int) (Math.random() * 1000000000));
	}

	/**
	 * @see org.seginfo.httpfuzzer.header.AttackStrategy#getStringAttack()
	 */
	public String getStringAttack() {
		return getStringAttack(0, (int) (Math.random() * 1000000000));
	}

	/**
	 * TODO por ahi esta implementacion deberia tomar cantidades fijas de caracteres a imprimir
	 */
	public String getStringAttack(int minLength, int maxLength) {
		int size = (int) (Math.random() * maxLength) + minLength;
		StringBuffer buffer = new StringBuffer();
		for (int i = 0; i < size; i++) {
			double p = Math.random();
			if (p < 0.2) {
				buffer.append("%x");
				i++;
			} else if (p < 0.4) {
				buffer.append("%n");
				i++;
			} else if (p < 0.6) {
				buffer.append("%s");
				i++;
			} else {
				buffer.append((char) ((Math.random() * 26) + 65));
			}
		}
		return buffer.toString();
	}

	/**
	 * TODO, hacer
	 */
	public String getHexAttack(int minLength, int maxLength) {
		int size = (int) (Math.random() * maxLength) + minLength;
		StringBuffer buffer = new StringBuffer();
		for (int i = 0; i < size; i++) {
			buffer.append(getRandomHex());
		}
		return buffer.toString();
	}
}
